package com.gs.SynPatrolRep.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;


import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.util.DateUtil;

import java.util.Calendar;
import java.util.Date;

public class LabelDateView extends LinearLayout {

    private Context context;
    private Calendar calendarStart, calendarEnd;
    private TextView etDateStart, etDateEnd;
    private String format;

    public LabelDateView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public LabelDateView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public LabelDateView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public LabelDateView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        this.context = context;
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LabelDateView);
        format = attributes.getString(R.styleable.LabelDateView_label_date_format);
        View view = LayoutInflater.from(context).inflate(R.layout.item_date, this, false);
        etDateStart = view.findViewById(R.id.et_date_start);
        etDateEnd = view.findViewById(R.id.et_date_end);
        OnClickListener onClickListener = new OnClickListener();
        view.findViewById(R.id.tv_date_start).setOnClickListener(onClickListener);
        view.findViewById(R.id.iv_date_end).setOnClickListener(onClickListener);
        calendarStart = Calendar.getInstance();
        calendarEnd = Calendar.getInstance();
        addView(view);
        attributes.recycle();
    }

    class OnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_date_start:
//                    DateUtil.getInstance().showDateSelector(context, v, calendarStart, format, new DateUtil.ShowTimeSelectorCallback() {
//                        @Override
//                        public void OnTimeSelectListener(Date date) {
//                            calendarStart.setTime(date);
//                            etDateStart.setText(DateUtil.getInstance().DateTimeToStr(date, format));
//                        }
//                    });
                    break;
                case R.id.iv_date_end:
//                    DateUtil.getInstance().showDateSelector(context, v, calendarEnd, format, new DateUtil.ShowTimeSelectorCallback() {
//                        @Override
//                        public void OnTimeSelectListener(Date date) {
//                            calendarEnd.setTime(date);
//                            etDateEnd.setText(DateUtil.getInstance().DateTimeToStr(date, format));
//                        }
//                    });
                    break;
            }
        }
    }

    public String getBat() {
        return etDateStart.getText().toString();
    }

    public String getEat() {
        return etDateEnd.getText().toString();
    }

    public void setBat(String date) {
        calendarStart.setTime(TextUtils.isEmpty(date) ? new Date() : DateUtil.getInstance().StrToDateTime(date,format));
        etDateStart.setText(date);
    }

    public void setEat(String date) {
        calendarEnd.setTime(TextUtils.isEmpty(date) ? new Date() : DateUtil.getInstance().StrToDateTime(date,format));
        etDateEnd.setText(date);

    }

    public void setNull(){
        etDateStart.setText("");
        etDateEnd.setText("");
        calendarStart.setTime(new Date());
        calendarEnd.setTime(new Date());
    }
}
